<html>
<head><meta charset="utf-8"><title>macro and syntax tree ranges · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20and.20syntax.20tree.20ranges.html">macro and syntax tree ranges</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="181154859"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20and%20syntax%20tree%20ranges/near/181154859" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremy Kolb <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20and.20syntax.20tree.20ranges.html#181154859">(Nov 19 2019 at 21:12)</a>:</h4>
<p>I'm a little confused. If I have an empty macro in my file:</p>
<div class="codehilite"><pre><span></span><span class="n">macro_rules</span><span class="o">!</span><span class="w"> </span><span class="n">id</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="p">()</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>Any nav target I generate falls outside of the range that shows up from the show sytnax tree command</p>



<a name="181155269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20and%20syntax%20tree%20ranges/near/181155269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremy Kolb <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20and.20syntax.20tree.20ranges.html#181155269">(Nov 19 2019 at 21:17)</a>:</h4>
<p>For instance this:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">        </span><span class="k">fn</span> <span class="nf">bar</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">fo</span><span class="o">&lt;|&gt;</span><span class="n">o</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>Generates  <code>foo FN_DEF FileId(1) [0; 11) [3; 6)</code>Which makes sense to me.. </p>
<div class="codehilite"><pre><span></span>FN_DEF@[0; 11)
    FN_KW@[0; 2) &quot;fn&quot;
    WHITESPACE@[2; 3) &quot; &quot;
    NAME@[3; 6)
      IDENT@[3; 6) &quot;foo&quot;
    PARAM_LIST@[6; 8)
      L_PAREN@[6; 7) &quot;(&quot;
      R_PAREN@[7; 8) &quot;)&quot;
    WHITESPACE@[8; 9) &quot; &quot;
    BLOCK_EXPR@[9; 11)
      BLOCK@[9; 11)
        L_CURLY@[9; 10) &quot;{&quot;
        R_CURLY@[10; 11) &quot;}&quot;
</pre></div>



<a name="181155472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20and%20syntax%20tree%20ranges/near/181155472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremy Kolb <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20and.20syntax.20tree.20ranges.html#181155472">(Nov 19 2019 at 21:19)</a>:</h4>
<p>While</p>
<div class="codehilite"><pre><span></span><span class="n">macro_rules</span><span class="o">!</span><span class="w"> </span><span class="n">id</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="p">()</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">            </span><span class="k">fn</span> <span class="nf">bar</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">fo</span><span class="o">&lt;|&gt;</span><span class="n">o</span><span class="p">();</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>Gives </p>
<div class="codehilite"><pre><span></span>SOURCE_FILE@[0; 140)
  MACRO_CALL@[0; 56)
    PATH@[0; 11)
      PATH_SEGMENT@[0; 11)
        NAME_REF@[0; 11)
          IDENT@[0; 11) &quot;macro_rules&quot;
    EXCL@[11; 12) &quot;!&quot;
    WHITESPACE@[12; 13) &quot; &quot;
    NAME@[13; 15)
      IDENT@[13; 15) &quot;id&quot;
    WHITESPACE@[15; 16) &quot; &quot;
    TOKEN_TREE@[16; 56)
      L_CURLY@[16; 17) &quot;{&quot;
      WHITESPACE@[17; 34) &quot;\n                &quot;
      TOKEN_TREE@[34; 36)
        L_PAREN@[34; 35) &quot;(&quot;
        R_PAREN@[35; 36) &quot;)&quot;
      WHITESPACE@[36; 37) &quot; &quot;
      EQ@[37; 38) &quot;=&quot;
      R_ANGLE@[38; 39) &quot;&gt;&quot;
      WHITESPACE@[39; 40) &quot; &quot;
      TOKEN_TREE@[40; 42)
        L_CURLY@[40; 41) &quot;{&quot;
        R_CURLY@[41; 42) &quot;}&quot;
      WHITESPACE@[42; 55) &quot;\n            &quot;
      R_CURLY@[55; 56) &quot;}&quot;
  WHITESPACE@[56; 69) &quot;\n            &quot;
  FN_DEF@[69; 80)
    FN_KW@[69; 71) &quot;fn&quot;
    WHITESPACE@[71; 72) &quot; &quot;
    NAME@[72; 75)
      IDENT@[72; 75) &quot;foo&quot;
    PARAM_LIST@[75; 77)
      L_PAREN@[75; 76) &quot;(&quot;
      R_PAREN@[76; 77) &quot;)&quot;
    WHITESPACE@[77; 78) &quot; &quot;
    BLOCK_EXPR@[78; 80)
      BLOCK@[78; 80)
        L_CURLY@[78; 79) &quot;{&quot;
        R_CURLY@[79; 80) &quot;}&quot;
  WHITESPACE@[80; 93) &quot;\n            &quot;
  FN_DEF@[93; 140)
    FN_KW@[93; 95) &quot;fn&quot;
    WHITESPACE@[95; 96) &quot; &quot;
    NAME@[96; 99)
      IDENT@[96; 99) &quot;bar&quot;
    PARAM_LIST@[99; 101)
      L_PAREN@[99; 100) &quot;(&quot;
      R_PAREN@[100; 101) &quot;)&quot;
    WHITESPACE@[101; 102) &quot; &quot;
    BLOCK_EXPR@[102; 140)
      BLOCK@[102; 140)
        L_CURLY@[102; 103) &quot;{&quot;
        WHITESPACE@[103; 120) &quot;\n                &quot;
        EXPR_STMT@[120; 126)
          CALL_EXPR@[120; 125)
            PATH_EXPR@[120; 123)
              PATH@[120; 123)
                PATH_SEGMENT@[120; 123)
                  NAME_REF@[120; 123)
                    IDENT@[120; 123) &quot;foo&quot;
            ARG_LIST@[123; 125)
              L_PAREN@[123; 124) &quot;(&quot;
              R_PAREN@[124; 125) &quot;)&quot;
          SEMI@[125; 126) &quot;;&quot;
        WHITESPACE@[126; 139) &quot;\n            &quot;
        R_CURLY@[139; 140) &quot;}&quot;
</pre></div>


<p>with <code>foo FN_DEF FileId(1) [33; 44) [36; 39)</code> That range is not within the fn def</p>



<a name="181155518"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20and%20syntax%20tree%20ranges/near/181155518" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremy Kolb <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20and.20syntax.20tree.20ranges.html#181155518">(Nov 19 2019 at 21:19)</a>:</h4>
<p>But goto def works in this case</p>



<a name="181181559"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20and%20syntax%20tree%20ranges/near/181181559" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20and.20syntax.20tree.20ranges.html#181181559">(Nov 20 2019 at 05:42)</a>:</h4>
<p><span class="user-mention" data-user-id="203366">@Jeremy Kolb</span> hm, <code>[33; 44)</code> is exactly the range of the fn node. Tests strip leadning indentation I think</p>



<a name="181200772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20and%20syntax%20tree%20ranges/near/181200772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremy Kolb <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20and.20syntax.20tree.20ranges.html#181200772">(Nov 20 2019 at 12:07)</a>:</h4>
<p>Why isn't it <code>[69; 80)</code>?</p>



<a name="181200908"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20and%20syntax%20tree%20ranges/near/181200908" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20and.20syntax.20tree.20ranges.html#181200908">(Nov 20 2019 at 12:09)</a>:</h4>
<p><a href="/user_uploads/4715/F6lo9DO220eQlKghxhJJQmtu/pasted_image.png" target="_blank" title="pasted_image.png">pasted image</a></p>
<div class="message_inline_image"><a href="/user_uploads/4715/F6lo9DO220eQlKghxhJJQmtu/pasted_image.png" target="_blank" title="pasted image"><img src="/user_uploads/4715/F6lo9DO220eQlKghxhJJQmtu/pasted_image.png"></a></div>



<a name="181200923"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20and%20syntax%20tree%20ranges/near/181200923" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20and.20syntax.20tree.20ranges.html#181200923">(Nov 20 2019 at 12:09)</a>:</h4>
<p><a href="/user_uploads/4715/qZQ2oB-wDo1WIWBnGjU2GJ6i/pasted_image.png" target="_blank" title="pasted_image.png">pasted image</a></p>
<div class="message_inline_image"><a href="/user_uploads/4715/qZQ2oB-wDo1WIWBnGjU2GJ6i/pasted_image.png" target="_blank" title="pasted image"><img src="/user_uploads/4715/qZQ2oB-wDo1WIWBnGjU2GJ6i/pasted_image.png"></a></div>



<a name="181200992"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20and%20syntax%20tree%20ranges/near/181200992" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20and.20syntax.20tree.20ranges.html#181200992">(Nov 20 2019 at 12:10)</a>:</h4>
<p>(33 is the start of the <code>fn</code></p>



<a name="181201324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20and%20syntax%20tree%20ranges/near/181201324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremy Kolb <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20and.20syntax.20tree.20ranges.html#181201324">(Nov 20 2019 at 12:15)</a>:</h4>
<p>So... then what do the ranges in the syntax tree represent?</p>



<a name="181201351"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20and%20syntax%20tree%20ranges/near/181201351" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremy Kolb <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20and.20syntax.20tree.20ranges.html#181201351">(Nov 20 2019 at 12:15)</a>:</h4>
<p>I've always assumed they were the character offsets.</p>



<a name="181201745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20and%20syntax%20tree%20ranges/near/181201745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20and.20syntax.20tree.20ranges.html#181201745">(Nov 20 2019 at 12:20)</a>:</h4>
<p>Character offsets</p>



<a name="181201763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20and%20syntax%20tree%20ranges/near/181201763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20and.20syntax.20tree.20ranges.html#181201763">(Nov 20 2019 at 12:20)</a>:</h4>
<p>33-44 is exactly the range of <code>fn foo() {}</code> (exactly 11 chars)</p>



<a name="181201918"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20and%20syntax%20tree%20ranges/near/181201918" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremy Kolb <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20and.20syntax.20tree.20ranges.html#181201918">(Nov 20 2019 at 12:23)</a>:</h4>
<p>Right... so why is it <code>FN_DEF@[69; 80)</code> above?</p>



<a name="181201960"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20and%20syntax%20tree%20ranges/near/181201960" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20and.20syntax.20tree.20ranges.html#181201960">(Nov 20 2019 at 12:23)</a>:</h4>
<p>Because it is indented</p>



<a name="181201964"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20and%20syntax%20tree%20ranges/near/181201964" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20and.20syntax.20tree.20ranges.html#181201964">(Nov 20 2019 at 12:23)</a>:</h4>
<p>if it is not indended, it's 33-44</p>



<a name="181202074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro%20and%20syntax%20tree%20ranges/near/181202074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremy Kolb <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro.20and.20syntax.20tree.20ranges.html#181202074">(Nov 20 2019 at 12:24)</a>:</h4>
<p>ah. gotcha</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>